package com.guoguo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.entity.InviteRelation;
import com.guoguo.enums.InviteEnum;
import com.guoguo.mapper.InviteRelationMapper;
import com.guoguo.service.InviteService;
import com.guoguo.utils.ClazzUtils;
import com.guoguo.utils.JsonUtils;
import com.guoguo.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2017/10/24 0024.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
@Service
public class InviteServiceImpl implements InviteService {
    @Autowired
    private InviteRelationMapper inviteRelationMapper;

    @Override
    public JSONObject saveOrUpdateInvite(String userId, String phone) {
        InviteRelation inviteRelation = new InviteRelation();
        inviteRelation.setSrcUserId(userId);
        inviteRelation.setReceptionPhone(phone);
        inviteRelation.setStatus(0);

        int num = saveOrUpdateInvite(inviteRelation);
        if (num > 0) {
            return JsonUtils.returnObject(InviteEnum.INVITE_SUCCESS.getId(), "", InviteEnum.INVITE_SUCCESS.getValue());
        } else {
            return JsonUtils.returnObject(InviteEnum.INVITE_ERROR.getId(), "", InviteEnum.INVITE_ERROR.getValue());
        }
    }

    /**
     * 新增或者修改邀请信息
     *
     * @param inviteRelation
     * @return
     */
    private int saveOrUpdateInvite(InviteRelation inviteRelation) {
        InviteRelation newInviteRelation1 = inviteRelationMapper.findInviteRelation(inviteRelation);
        int num = 0;
        if (newInviteRelation1 == null) {
            newInviteRelation1 = (InviteRelation) ClazzUtils.copyBean(inviteRelation, new InviteRelation());
            newInviteRelation1.setInviteId(UUIDUtils.getUUID());
            newInviteRelation1.setCreateTime(new Date());
            num = inviteRelationMapper.insert(newInviteRelation1);
        } else {
            newInviteRelation1.setUpdateTime(new Date());
            num = inviteRelationMapper.updateByPrimaryKey(newInviteRelation1);
        }
        return num;
    }


}
